Lecture : Hello Python!


  • [RQ-1] : Which of the following statements is correct?

    Ans: The Ipython Shell is typically used to work with Python interactively.

  • [RQ-2] : Which file extension is used for Python script files?**

    Ans: .py

  • [RQ-3] : You need to print the result of adding 3 and 4 inside a script. Which line of code should you write in the script?

    Ans: print(int x + int y)


Lab : Hello Python!


Objective :

  • How to work with Ipython shell.
  • Writing python scripts.

The Python Interface -- 100xp, Status : Earned


In [1]:
# working with print function
print(5 / 8)

# Add another print function on new line
print(7 + 10)


0.625
17

When to use python? -- 50xp, Status : Earned


Python is a pretty versatile language. For what applications can you use Python?

Ans: All of the above


Any comments? -- 100xp, Satatus : Earned


  • We can add comments to python scripts.

  • Comments are short snippets of plain english, to help you and others understand what the code is about.

  • To add a comment, use '#'tag, insert it at the front of the text.

  • Comments have idle state, i.e. they don't affect the code results.

  • Comments are ignored by the python interpretor.



In [2]:
# Just testing division
print(5 / 8)

# Additon works too ( added comment here )
print(7 + 10)


0.625
17

Python as a calculator -- 100xp, Status : Earned


Python is perfectly suited to do basic calculations. Apart from addition, subtraction, multiplication and division, there is also support for more advanced operations such as:

  • Exponentiation:. This operator raises the number to its left to the power of the number to its right: for example 42 will give 16.
  • Modulo: %. It returns the remainder of the division of the number to the left by the number on its right, for example 18 % 7 equals 4.


In [9]:
"""Suppose you have $100, which you can invest with a 10% return each year. After one year, it's 
100 x 1.1 = 110 dollars, and after two years it's 100 x 1.1 x 1.1 = 121.

Add code to calculate how much money you end up with after 7 years"""

print(5 + 5)
print(5 - 5)

# Multiplication and division
print(3 * 5)
print(10 / 2)

# Exponentiation
print(4 ** 2)

# Modulo
print(18 % 7)

# How much is your $100 worth after 7 years?
# first try was unsuccesful, so used the only two things * and ** operators.
print ( 100 * ( 1.1 ** 7 ) )


10
0
15
5.0
16
4
194.87171000000012